package android.rockchip.update.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * Class  Name: RKUpdateReceiver
 * Description:
 * Created by bruce on 17/10/9
 */
public class RKUpdateReceiver extends BroadcastReceiver {
    private final static String TAG = "RKUpdateReceiver";
    private static boolean isBootCompleted = false;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e(TAG, "action = " + action);
        Intent serviceIntent;
        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            Log.e(TAG, "RKUpdateReceiver recv ACTION_BOOT_COMPLETED.");
            serviceIntent = new Intent("android.rockchip.update.service");
            serviceIntent.setPackage("android.rockchip.update.service");
            serviceIntent.putExtra("command", RKUpdateService.COMMAND_CHECK_LOCAL_UPDATING);
            serviceIntent.putExtra("delay", 20000);
            context.startService(serviceIntent);

            serviceIntent = new Intent("android.rockchip.update.service");
            serviceIntent.setPackage("android.rockchip.update.service");
            serviceIntent.putExtra("command", RKUpdateService.COMMAND_CHECK_REMOTE_UPDATING);
            serviceIntent.putExtra("delay", 25000);
            context.startService(serviceIntent);
            isBootCompleted = true;
        } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED) && isBootCompleted) {
            String[] path = {intent.getData().getPath()};
            serviceIntent = new Intent("android.rockchip.update.service");
            serviceIntent.setPackage("android.rockchip.update.service");
            serviceIntent.putExtra("command", RKUpdateService.COMMAND_CHECK_LOCAL_UPDATING);
            serviceIntent.putExtra("delay", 5000);
            context.startService(serviceIntent);
            Log.e(TAG, "media is mounted to '" + path[0] + "'. To check local update.");
        } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) && isBootCompleted) {
            ConnectivityManager cmanger = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cmanger.getActiveNetworkInfo();
            if (netInfo != null) {
                if ((netInfo.getType() == ConnectivityManager.TYPE_WIFI
                        || (netInfo.getType() == ConnectivityManager.TYPE_ETHERNET)) && netInfo.isConnected()) {
                    serviceIntent = new Intent("android.rockchip.update.service");
                    serviceIntent.setPackage("android.rockchip.update.service");
                    serviceIntent.putExtra("command", RKUpdateService.COMMAND_CHECK_REMOTE_UPDATING);
                    serviceIntent.putExtra("delay", 5000);
                    context.startService(serviceIntent);
                }
            }
        } else if (action.equals(UsbManager.ACTION_USB_DEVICE_DETACHED) && isBootCompleted) {
//            Bundle extras = intent.getExtras();
//            boolean connected = extras.getBoolean(UsbManager.USB_CONNECTED);
//            boolean configured = extras.getBoolean(UsbManager.USB_CONFIGURED);
//            boolean mtpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_MTP);
//            boolean ptpEnabled = extras.getBoolean(UsbManager.USB_FUNCTION_PTP);
//            // Start MTP service if USB is connected and either the MTP or PTP function is enabled
//            if ((!connected) && mtpEnabled && (!configured)) {
            serviceIntent = new Intent("android.rockchip.update.service");
            serviceIntent.setPackage("android.rockchip.update.service");
            serviceIntent.putExtra("command", RKUpdateService.COMMAND_CHECK_LOCAL_UPDATING);
            serviceIntent.putExtra("delay", 5000);
            context.startService(serviceIntent);
//            }
        }
    }

}


